#! /bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script!"
exit 1
fi

echo "+-------------------------------------------+"
echo "|             Manager for WebApp            |"
echo "+-------------------------------------------+"

arg1=$1
arg2=$2

echo $arg1;
echo $arg2;

APP_DIR=/www/python/host/
# 统计脚本
AXE_APP_DIR=/www/python/Axe/

uwsgi_start() {
    echo "Starting uWSGI..."
    if [ -f "/tmp/uwsgi.pid" ]; then
        uwsgi --stop /tmp/uwsgi.pid >/dev/null 2>&1
    fi
    if [ -f "/tmp/axe.pid" ]; then
        uwsgi --stop /tmp/axe.pid >/dev/null 2>&1
    fi
    pkill -9 uwsgi
    nohup uwsgi --ini ${APP_DIR}config.ini >/dev/null 2>&1 &
    if [ ${HOSTNAME}=="lixtix0osmapi01" ]; then
        cd ${APP_DIR}scheduler
        ./run.sh
        echo "******counter has started******"
        cd -
        nohup uwsgi --ini ${AXE_APP_DIR}config.ini >/dev/null 2>&1 &
    fi
}

uwsgi_test() {
    echo "Starting uWSGI..."
    pkill -9 uwsgi
    uwsgi --ini ${APP_DIR}config.ini
    if [ ${HOSTNAME}=="lixtix0osmapi01" ]; then
        cd ${APP_DIR}scheduler
        ./run.sh
        echo "******counter has started******"
        cd -
        nohup uwsgi --ini ${AXE_APP_DIR}config.ini >/dev/null 2>&1 &
    fi
}

uwsgi_stop() {
    echo "Stoping UWSGI..."
    if [ -f "/tmp/uwsgi.pid" ]; then
        uwsgi --stop /tmp/uwsgi.pid >/dev/null 2>&1
    else
        pkill -9 uwsgi
    fi
    if [ ${HOSTNAME}=="lixtix0osmapi01" ]; then
        if [ -f "/tmp/axe.pid" ]; then
            uwsgi --stop /tmp/axe.pid >/dev/null 2>&1
        else
            pkill -9 uwsgi
        fi
    fi
}

uwsgi_reload() {
    echo "Reload UWSGI..."
    uwsgi --reload /tmp/uwsgi.pid
    if [ ${HOSTNAME}=="lixtix0osmapi01" ]; then
        uwsgi --reload /tmp/axe.pid
    fi
}

webapp_start()
{
    echo "Starting webapp..."
    /etc/init.d/nginx start
    uwsgi_start
}

webapp_stop()
{
    echo "Stoping webapp..."
    /etc/init.d/nginx stop
    uwsgi_stop
}

webapp_reload()
{
    echo "Reload webapp..."
    /etc/init.d/nginx reload
    uwsgi_reload
}

webapp_kill()
{
    echo "Kill nginx,uWSGI process..."
    killall nginx
    killall uwsgi
    echo "done."
}

webapp_status()
{
    /etc/init.d/nginx status
    if [ -f "/tmp/uwsgi.pid" ]; then
        echo "uwsgi is runing!"
    else
        echo "uwsgi is stop!"
    fi
}

case "${arg1}" in
    start)
        webapp_start
        /etc/init.d/mysql start
        ;;
    stop)
        webapp_stop
        /etc/init.d/mysql stop
        ;;
    restart)
        webapp_stop
        sleep 1s
        webapp_start
        /etc/init.d/mysql restart
        ;;
    reload)
        webapp_reload
        /etc/init.d/mysql reload
        ;;
    kill)
        webapp_kill
        killall mysql
        ;;
    status)
        webapp_status
        /etc/init.d/mysql status
        ;;
    uwsgi)
        if [ ${arg2}=="start" ]; then
            uwsgi_start
        elif [ ${arg2}=="stop" ]; then
            uwsgi_stop
        elif [ ${arg2}=="reload" ]; then
            uwsgi_reload
        fi
        ;;
    nginx)
        /etc/init.d/nginx ${arg2}
        ;;
    mysql)
        /etc/init.d/mysql ${arg2}
        ;;
    test)
        /etc/init.d/nginx stop
        /etc/init.d/mysql stop
        pkill -9 uwsgi
        sleep 1s
        /etc/init.d/nginx start
        /etc/init.d/mysql start
        uwsgi_test
        ;;
    *)
        echo "Usage: webapp {start|stop|reload|restart|kill|status}"
        echo "Usage: webapp test"
        echo "Usage: webapp uwsgi {start|stop|reload}"
        echo "Usage: webapp nginx {start|stop|reload|restart|kill|status}"
        echo "Usage: webapp mysql {start|stop|reload|force-reload|restart|status}"
esac
exit
